package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.pojo.vo.BlogTypeCountVO;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// Alt+Enter
@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    //在Service层封装分页需要的LayUITableResult
    @Override
    public LayUITableResult list(BlogQuery blogQuery) {
        //使用PageHelper开启分页
        PageHelper.startPage(blogQuery.getPage(), blogQuery.getLimit());
        //开启分页之后，查找所有会自动带上分页
        List<Blog> list = blogMapper.list(blogQuery);
        //获取总的数量，目的是为了算分页
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        return LayUITableResult.ok((int)totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            blogMapper.deleteById(id);
        }
    }

    @Override
    public void add(Blog blog) {
        blogMapper.add(blog);
    }

    @Override
    public List<BlogTypeCountVO> selectBlogTypeCount() {
        return blogMapper.selectBlogTypeCount();
    }

    /*@Override
    public List<Blog> list() {
        List<Blog> list = blogMapper.list();
        for (Blog blog : list) {
            blog.setName("situ-" + blog.getName());
        }
        return list;

        //return blogMapper.list();
    }*/
}
